/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.model.dbsql.modelCache;

import com.dc.gameserver.extComponents.springDataRedisCache.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author 石头哥哥
 *         Date: 13-11-25</br>
 *         Time: 下午4:43</br>
 *         Package: com.dc.gameserver.serverCore.model.dbsql.modelService</br>
 *         注解：数据库持久vo，加入redis缓存模块，处理策略如下：
 *         1.当更新（添加，删除）数据，同步操作数据库和redis缓存两个模块部分，先更新数据库，然后更新缓存，保证缓存和数据库的数据一致性；
 *         2.查询数据，优先查找redis缓存模块，如果redis中没有数据，那么直接加载数据库，并将数据缓存在redis；
 *         3.当玩家下线之后，先更新数据库，在清理缓存数据（定时清理）；
 *
 *         此处封装redis的操作接口,用于存储数据库modelVo
 */

@Service
public  class ModelCacheService implements imodelCacheService{

    @Resource
    private RedisServiceList redisServiceList;

    @Resource
    private RedisServiceMap redisServiceMap;

    @Resource
    private RedisServiceObject redisServiceObject;

    @Resource
    private RedisServiceStringList redisServiceStringList;

    @Resource
    private RedisServiceStringMap redisServiceStringMap;

    @Resource
    private RedisServiceStringObject redisServiceStringObject;


    /**
     * for redis
     * 数据 region
     */
    public static class DATABASE_PREFIX_ID{
        protected static String ROLE_ID="t_role";         //角色表   region
        protected static String EQU_ID="t_equ";           //装备表   region
        protected static String BOOK_ID="t_book";         //秘籍表   region
        protected static String PROPS_ID="t_props";        //道具表  region
        protected static String AREA_ID="t_area";           //area表   region
        protected static String POINT_PROGRASS_ID="t_level"; //关卡表  region
        protected static String SPRITE_ID="t_sprite";              //灵脉 灵泉 蒲团
    }

    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String SPRITE_PREFIX(int roleID){
        return DATABASE_PREFIX_ID.SPRITE_ID + roleID;
    }
    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String ROLE_PREFIX(int roleID){
        return DATABASE_PREFIX_ID.ROLE_ID + roleID;
    }

    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String EQU_PREFIX(int roleID){
        return    DATABASE_PREFIX_ID.EQU_ID+roleID;
    }

    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String BOOK_PREFIX(int roleID){
        return    DATABASE_PREFIX_ID.BOOK_ID+roleID;
    }

    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String PROPS_PREFIX(int roleID){
        return    DATABASE_PREFIX_ID.PROPS_ID+roleID;
    }

    /**
     * 获取AREA  region
     * @param roleID
     * @return
     */
    public static String AREA_PREFIX(int roleID){
        return    DATABASE_PREFIX_ID.AREA_ID+roleID;
    }

    /**
     *  获取table前缀
     * @param roleID
     * @return
     */
    public static String POINT_PROGRASS_PREFIX(int roleID){
        return    DATABASE_PREFIX_ID.POINT_PROGRASS_ID+roleID;
    }


    /**
     * @param key
     * @param value 其中 list中的数据应该实现序列化接口
     */
    @Override
    public void addObject(String key, String value) {
        redisServiceObject.set(key, value);
    }

    /**
     * @return
     */
    @Override
    public String find(String key) {
        return redisServiceObject.get(key);  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * 删除 缓存 key对应的value
     *
     * @param key
     */
    @Override
    public void delete(String key) {
        redisServiceObject.del(key);
    }

    /**
     * RedisServiceObject
     *
     * @param ID
     */
    @Override
    public void addObject(int ID, Serializable obj) {
       redisServiceObject.set(ID, obj);
    }

    /**
     * RedisServiceObject
     *
     * @param ID
     * @return
     */
    @Override
    public Serializable find(int ID) {
        return redisServiceObject.get(ID);
    }

    /**
     * RedisServiceObject
     *
     * @param ID
     * @return
     */
    @Override
    public void delete(int ID) {
        redisServiceObject.del(ID);
    }

    /**
     * RedisServiceList
     *
     * @param ID
     * @param list
     */
    @Override
    public void addList(int ID, List<?> list) {
        redisServiceList.set(ID, list);
    }

    /**
     * RedisServiceList
     *
     * @param ID
     * @return
     */
    @Override
    public List<?> findList(int ID) {
        return redisServiceList.get(ID);
    }

    /**
     * RedisServiceList
     *
     * @param ID
     */
    @Override
    public void deleteLsit(int ID) {
        redisServiceList.del(ID);//To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * RedisServiceMap
     *
     * @param ID
     * @param map
     */
    @Override
    public void addMap(int ID, Map<?, ?> map) {
        redisServiceMap.set(ID, map);//To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * RedisServiceMap
     *
     * @param ID
     * @return
     */
    @Override
    public Map<?, ?> findMap(int ID) {
        return redisServiceMap.get(ID);  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * RedisServiceMap
     *
     * @param ID
     */
    @Override
    public void deleteMap(int ID) {
        redisServiceMap.del(ID);//To change body of implemented methods use File | Settings | File Templates.
    }



    /*********prefix+roleID**************************************/

    /**
     * RedisServiceObject
     *
     * @param prefix_roleID
     */
    @Override
    public void addObjectPrefixID(String prefix_roleID, Serializable obj) {
           redisServiceStringObject.set(prefix_roleID, obj);
    }

    /**
     * RedisServiceObject
     *
     * @param prefix_roleID
     * @return
     */
    @Override
    public Serializable findByPrefixID(String prefix_roleID) {
        return redisServiceStringObject.get(prefix_roleID);
    }

    /**
     * RedisServiceObject
     *
     * @param prefix_roleID
     * @return
     */
    @Override
    public void deleteByPrefixID(String prefix_roleID) {
        redisServiceStringObject.del(prefix_roleID);
    }

    /**
     * RedisServiceList
     *
     * @param prefix_roleID
     * @param list
     */
    @Override
    public void addListByPrefixID(String prefix_roleID, List<?> list) {
        redisServiceStringList.set(prefix_roleID, list);
    }

    /**
     * RedisServiceList
     *
     * @param prefix_roleID
     * @return
     */
    @Override
    public List<?> findListByPrefixID(String prefix_roleID) {
        return redisServiceStringList.get(prefix_roleID);
    }

    /**
     * RedisServiceList
     *
     * @param prefix_roleID
     */
    @Override
    public void deleteLsitByPrefixID(String prefix_roleID) {
      redisServiceStringList.del(prefix_roleID);
    }

    /**
     * RedisServiceMap
     *
     * @param prefix_roleID
     * @param map
     */
    @Override
    public void addMapByPrefixID(String prefix_roleID, Map<?, ?> map) {
       redisServiceStringMap.set(prefix_roleID, map);
    }

    /**
     * RedisServiceMap
     *
     * @param prefix_roleID
     * @return
     */
    @Override
    public Map<?, ?> findMapByPrefixID(String prefix_roleID) {
        return redisServiceStringMap.get(prefix_roleID);
    }

    /**
     * RedisServiceMap
     *
     * @param prefix_roleID        prefix+roleID
     */
    @Override
    public void deleteMapByPrefixID(String prefix_roleID) {
       redisServiceStringMap.del(prefix_roleID);
    }

}
